package com.woniuxy.ams.service.impl;

import com.woniuxy.ams.dao.DeptDao;
import com.woniuxy.ams.dao.UserDao;
import com.woniuxy.ams.entity.Dept;
import com.woniuxy.ams.entity.User;
import com.woniuxy.ams.service.UserService;
import com.woniuxy.ams.utils.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author fyx
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private DeptDao deptDao;

    @Override
    public List<User> findAllAndSearch(User user) {
        return userDao.findAllAndSearch(user);
    }

    @Override
    public User findOneByName(String name) {
        return userDao.findOneByName(name);
    }

    @Override
    public User findOneById(int id) {
        return userDao.findOneById(id);
    }

    @Override
    public boolean deleteUserByName(String name) {
        User user = userDao.findOneByName(name);
        if (user != null) {
            int isSuccess = userDao.updateStatus(user.getId());
            return isSuccess != 0;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean addUser(User user) {
        User userIfExists = userDao.findOneByName(user.getName());
        if (userIfExists != null) {
            log.warn("该用户已存在: " + user.getName());
            return false;
        }
        if (deptDao.findOneById(user.getDeptId()) == null) {
            log.warn("该用户对应的部门编号不存在: " + user.getDeptId());
            return false;
        }
        Integer creatorId = Objects.requireNonNull(SystemUtils.getLoginUser()).getId();
        user.setCreator(creatorId);
        user.setUpdated(creatorId);
        SimpleHash hash = new SimpleHash("MD5", user.getPassword(), user.getName() + "simpleWoNiu");
        user.setPassword(hash.toHex());
        return userDao.addUser(user) != 0;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean deleteUserByIds(List<Integer> ids) {
        return userDao.updateStatusByIds(ids);
    }

    @Override
    public boolean resetPassword(User user) {
        user.setUpdated(SystemUtils.getLoginUser().getId());
        user.setPassword(new SimpleHash("MD5", "123", user.getName() + "simpleWoNiu").toHex());
        return userDao.setPassword(user);
    }

    @Override
    public boolean setPassword(User user) {
        SimpleHash hash = new SimpleHash("MD5", user.getPassword(), user.getName() + "simpleWoNiu");
        user.setPassword(hash.toHex());
        user.setUpdated(SystemUtils.getLoginUser().getId());
        return userDao.setPassword(user);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean updateByUser(User user) {
        User userInDb = userDao.findOneByName(user.getName());
        Dept deptInDb = deptDao.findOneById(user.getDeptId());
        // 判断数据库中是否存在该user
        if (userInDb == null || deptInDb == null) {
            log.warn("该用户不存在或该部门不存在" + user);
            return false;
        }
        // 判断部门是否存在改变
        if (user.getDeptId().equals(userInDb.getDeptId())) {
            user.setDeptId(null);
        }
        user.setId(userInDb.getId());
        // 判断传入用户名是否改变
        if (!user.getName().equals(userInDb.getName())) {
            // 重置密码
            user.setPassword(new SimpleHash("MD5", "123", user.getName() + "simpleWoNiu").toHex());
        } else {
            user.setName(null);
        }
        Integer loginUserId = Objects.requireNonNull(SystemUtils.getLoginUser()).getId();
        user.setUpdated(loginUserId);
        return userDao.updateUser(user);
    }

    @Override
    public List<User> findUsersByGroup(User data, String type) {
        return userDao.findUserByGroup(data, type);
    }


}
